package tp.view;

import tp.model.NucleoCeleste;
import tp.controller.Controlador;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Observable;
import java.util.Observer;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

/**
 * Esta es la clase que representa a la vista del misil
 * 
 * @author Dr Von Nostrum
 * 
 */
public class NucleoVistaCeleste extends Component implements Observer {

	private Graphics g;

	public NucleoCeleste nucleo;

	public BufferedImage nucleoImage;

	protected String[] spriteNames;

	protected int currentFrame;

	private int height;

	private int width;

	public NucleoVistaCeleste(NucleoCeleste unNucleo) {
		nucleo = unNucleo;
		g = this.getGraphics();
		setSpriteNames(new String[] { "NucleoCeleste.jpg" });
		currentFrame = 0;
	}

	private void checkOffscreenImage() {
		// Dimension d = getSize();
		Dimension d = CopyOfBoardView1.panel.getSize();
		InputStream in = getClass().getResourceAsStream("NucleoCeleste.jpg");
		JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
		try {
			nucleoImage = decoder.decodeAsBufferedImage();
			in.close();
		} catch (Exception ex) {

		}

	}

	public void paint(Graphics g) {
		if (nucleo != null) {
			Dimension d = CopyOfBoardView1.panel.getSize();// Dimension d =
															// this.getParent().getSize();
			// checkOffscreenImage();
			// Graphics offG = misilImage.getGraphics();
			// offG.setColor(Color.GRAY);// getBackground());
			// offG.drawImage(misilImage, 0, 0, d.width, d.height, null);
			// offG.fillRect(0, 0, d.width, d.height);

			// Dibujo la imagen propieamente dicha.
			// paintOffscreen(nucleoImage.getGraphics());

			// Renderizo la imagen finalmente en la pantalla.
			if (nucleo != null) {
				if (nucleo.isVivo() == true) {
					g.drawImage(Controlador.spriteCache
							.getSprite(spriteNames[currentFrame]), nucleo
							.getMiPosicion().getXInt()
							- (int) nucleo.getMiAncho() / 2, d.height
							- nucleo.getMiPosicion().getYInt()
							- (int) nucleo.getMiAlto() / 2, (int) nucleo
							.getMiAncho(), (int) nucleo.getMiAlto(), null);
				}
			}
		}
	}

	/*
	 * public void paintOffscreen(Graphics g) { Dimension d =
	 * CopyOfBoardView1.panel.getSize(); if (nucleo!=null){ if
	 * (nucleo.isVivo()==true){ g.drawImage(nucleoImage,
	 * nucleo.getMiPosicion().getXInt()-(int)nucleo.getMiAncho()/2, d.height -
	 * nucleo.getMiPosicion().getYInt()-(int)nucleo.getMiAlto()/2,
	 * (int)nucleo.getMiAncho(),(int)nucleo.getMiAlto(), null); } } }
	 */

	public void setSpriteNames(String[] names) {
		spriteNames = names;
		height = 0;
		width = 0;
		for (int i = 0; i < names.length; i++) {
			BufferedImage image = Controlador.spriteCache
					.getSprite(spriteNames[i]);
			height = Math.max(height, image.getHeight());
			width = Math.max(width, image.getWidth());
		}
	}

	public void update(Observable unNucleoAzul, Object arg1) {
		if (nucleo != null) {
			if (nucleo.getMiPosicion() != null) {
				g = this.getGraphics();
				Dimension d = this.getParent().getSize();
			} else {
				nucleo = null;
			}
		}
	}

	public void Destruir() {
		this.nucleo = null;
	}
}